(function () {
	window.initAchievementsPage = function (app, position, size) {
		var layer = new Kinetic.Layer();
	    layer.add(createBackground(size));
		layer.add(createCloseButton(app, size));
		layer.add(createTitle(size));
		layer.add(createAchievementsRepresentation(app, position));
	    var stage = new Kinetic.Stage({
	        container: 'achievementsPage',
	        x: position.x,
	        y: position.y,
	        width: position.x + size.width,
	        height: position.y + size.height
	    });
	    stage.add(layer);
	    return stage;
	};

	function createAchievementsRepresentation (app, basePosition) {
		var g = new Kinetic.Group();
		for (var i in app.achievements) {
			g.add(createAchievementRepresentation(app, basePosition, i, app.achievements[i]));
		}
		return g;
	}
	function createAchievementRepresentation (app, basePosition, index, achievement) {
		var g = new Kinetic.Group();
		var y = basePosition.y + 60 + index * 60;
		g.add(createButton({
			x: basePosition.x + 20,
			y: y,
			width: 100,
			height: 40,
			text: achievement.name
		}));
		g.add(createButton({
			x: basePosition.x + 130,
			y: y,
			width: 60,
			height: 40,
			text: achievement.episode + (app.currentUser.level >= achievement.level ? '!!' : '')
		}));
		g.add(createButton({
			x: basePosition.x + 200,
			y: y,
			width: 80,
			height: 40,
			text: '$' + achievement.money + (app.currentUser.money >= achievement.money ? '!!' : '')
		}));
		g.add(createButton({
			x: basePosition.x + 290,
			y: y,
			width: 80,
			height: 40,
			text: 'IF: ' + achievement.invited_friends + (app.currentUser.invited_friends >= achievement.invited_friends ? '!!' : '')
		}));
		g.add(createButton({
			x: basePosition.x + 380,
			y: y,
			width: 90,
			height: 40,
			text: 'LFC: ' + achievement.levels_fast_completed + (app.currentUser.levels_fast_completed >= achievement.levels_fast_completed ? '!!' : '')
		}));
		g.add(createButton({
			x: basePosition.x + 480,
			y: y,
			width: 90,
			height: 40,
			text: '+' + achievement.bonus
		}));
		return g;
	}
	function createBackground (pageSize) {
		return createRect(0, 0, pageSize.width - 20, pageSize.height - 20);
	}
	function createCloseButton (app, pageSize) {
		var btn = createButton({
			x: pageSize.width - 140, 
			y: 20, 
			width: 100, 
			height: 40, 
			text: 'Закрыть'
		});
		btn.on('click', function () {
			app.moveToBottom('achievementsPage');
		});
		return btn;
	}
	function createTitle (pageSize) {
		return createText(pageSize.width / 2 - 130, 20, 45, 'Достижения');
	}

	function createButton (btn) {
		var button = new Kinetic.Group();
		button.add(createRect(btn.x, btn.y, btn.width, btn.height));
		button.add(createText(btn.x, btn.y, 20, btn.text));
		button.on('mouseover', function() {
			document.body.style.cursor = 'pointer';
		});
		button.on('mouseout', function() {
			document.body.style.cursor = 'default'; 
		});
		return button;
	}
	function createRect (x, y, width, height) {
		return window.EngineWrapper.rect(x, y, width, height);
	}
	function createText (x, y, fontSize, text) {
		var el = new Kinetic.Text({
			x: x,
			y: y,
			text: text,
			fontSize: fontSize,
			fill: '#555',
			padding: 10,
			align: 'center'
		});
		return el;
	}
})();